package com.campus.dao.impl;

import com.campus.dao.ClassDao;
import com.campus.model.Class;
import com.campus.model.Student;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
/**
 * @desc:
 * @author: Tournesol234
 * @date: 2024/11/22 14.41
 */
@Repository
public class ClassDaoImpl implements ClassDao {
    private final JdbcTemplate jdbcTemplate;

    public ClassDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int createClass(Class cls) {
        String sql = "INSERT INTO t_manager_class (name, created_time, class_desc, teacher_id, activity_id) VALUES (?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, cls.getName(), cls.getCreatedTime(), cls.getClassDesc(), cls.getTeacherId(), cls.getActivityId());
    }

    @Override
    public int updateClass(Class cls) {
        String sql = "UPDATE t_manager_class SET name = ?, class_desc = ?, teacher_id = ?, activity_id = ? WHERE id = ?";
        return jdbcTemplate.update(sql, cls.getName(), cls.getClassDesc(), cls.getTeacherId(), cls.getActivityId(), cls.getId());
    }

    @Override
    public int addStudentsToClass(int classId, List<Integer> studentIds) {
        if (studentIds == null || studentIds.isEmpty()) {
            return 0;
        }

        String sql = "INSERT INTO t_manager_class_student (class_id, student_id) VALUES (?, ?)";
        int rowsAffected = 0;
        try {
            for (Integer studentId : studentIds) {
                rowsAffected += jdbcTemplate.update(sql, classId, studentId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return rowsAffected;
    }


    @Override
    public int assignTeachersToClass(int classId, List<Integer> teacherIds) {
        String sql = "INSERT INTO t_manager_class_teacher (class_id, teacher_id) VALUES (?, ?)";
        int rowsAffected = 0;
        for (Integer teacherId : teacherIds) {
            rowsAffected += jdbcTemplate.update(sql, classId, teacherId);
        }
        return rowsAffected;
    }

    @Override
    public Class getClassById(int id) {
        String sql = "SELECT * FROM t_manager_class WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, (rs, rowNum) -> {
            Class cls = new Class();
            cls.setId(rs.getInt("id"));
            cls.setName(rs.getString("name"));
            cls.setCreatedTime(rs.getTimestamp("created_time"));
            cls.setClassDesc(rs.getString("class_desc"));
            cls.setTeacherId(rs.getInt("teacher_id"));
            cls.setActivityId(rs.getInt("activity_id"));
            return cls;
        });
    }

    @Override
    public List<Class> getClassesByActivityId(int activityId) {
        String sql = "SELECT * FROM t_manager_class WHERE activity_id = ?";
        return jdbcTemplate.query(sql, new Object[]{activityId}, (rs, rowNum) -> {
            Class cls = new Class();
            cls.setId(rs.getInt("id"));
            cls.setName(rs.getString("name"));
            cls.setCreatedTime(rs.getTimestamp("created_time"));
            cls.setClassDesc(rs.getString("class_desc"));
            cls.setTeacherId(rs.getInt("teacher_id"));
            cls.setActivityId(rs.getInt("activity_id"));
            return cls;
        });
    }

    @Override
    public List<Class> getAllClasses() {
        String sql = "SELECT * FROM t_manager_class";
        return jdbcTemplate.query(sql, (rs, rowNum) -> {
            Class cls = new Class();
            cls.setId(rs.getInt("id"));
            cls.setName(rs.getString("name"));
            cls.setCreatedTime(rs.getTimestamp("created_time"));
            cls.setClassDesc(rs.getString("class_desc"));
            cls.setTeacherId(rs.getInt("teacher_id"));
            cls.setActivityId(rs.getInt("activity_id"));
            return cls;
        });
    }

    @Override
    public int deleteClass(int classId) {
        String sql = "DELETE FROM t_manager_class WHERE id = ?";
        return jdbcTemplate.update(sql, classId);
    }
}
